//
//  NetworkServerManager.swift
//  PerfectTemplatePackageDescription
//
//  Created by John on 2018/6/6.
//

import Foundation
import PerfectHTTP
import PerfectLib

public func configure() -> Routes {
    
    var routes = Routes()
    
    routes.add(method: .get, uri: "/selectUserInfor") { (request, response) in
        let path = request.path
        print(path)
        
        let queryParams = request.queryParams
        if queryParams.count == 0{
            let result = DataBaseManager().mysqlGetHomeDataResult()
            
            let jsonString = baseResponseBodyJSONData(code: 200, message: "成功", data: result)
            
            response.setBody(string: jsonString)
            
            response.completed()
        }else{
            //有参数
            //let value : String
            for i in 0...queryParams.count - 1{
                let partArr = queryParams[i]
                print(partArr)
            }
            let jsonString = baseResponseBodyJSONData(code: 200, message: "成功", data: nil)
            response.setBody(string: jsonString)
            response.completed()
        }
    }
    return routes
}
func baseResponseBodyJSONData(code:Int,message:String,data:[Dictionary<String, String>]?) -> String{
    let result = ["code":code,"message":message,"data":data as Any] as [String : Any]
    var encoded:String = "初始化"
    do{
        encoded = try result.jsonEncodedString()
        return encoded
    }
    catch{
        
    }
    return encoded
}

